﻿using System;
using System.Collections.Generic;
using Application.MainModule.BankAccountsManagement;
using Domain.MainModule.Entities;
using Infraestructure.CrossCutting.IoC;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Application.MainModule.Tests
{
    /// <summary>
    ///This is a test class for BankAccountManagementServiceTest and is intended
    ///to contain all BankAccountManagementServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class BankAccountManagementServiceTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for Add BankAccount
        ///
        ///</summary>
        [TestMethod()]
        public void AddBankAccount_Invoke_Test()
        {
            //Arrange
            IBankAccountManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IBankAccountManagementService>();
            Guid userId = Guid.NewGuid();
            BankAccount bankAccount = new BankAccount()
            {
                IsEnabled = true,
                UserId = userId
            };

            //Act
            target.AddBankAccount(bankAccount);
            List<BankAccount> result = target.FindBankAccountsByUserId(userId);

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(userId, result[0].UserId);
            target.RemoveBankAccount(result[0]);
        }

        /// <summary>
        ///A test for ChangeBankAccount
        ///</summary>
        [TestMethod()]
        public void ChangBankAccount_Invoke_Test()
        {
            //Arrange
            IBankAccountManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IBankAccountManagementService>();
            Guid userId = Guid.Parse("ABCDF000-1234-5678-9000-123456789000");

            //Act
            List<BankAccount> bankAccount = target.FindBankAccountsByUserId(userId);
            string oldValue = bankAccount[0].BankAccountNumber;
            bankAccount[0].BankAccountNumber = "777";
            target.ChangeBankAccount(bankAccount[0]);
            List<BankAccount> result = target.FindBankAccountsByUserId(userId);

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.AreNotEqual(oldValue, result[0].BankAccountNumber);
        }

        /// <summary>
        ///A test for FindBankAccountByUserId
        ///</summary>
        [TestMethod()]
        public void FindBankAccountByUserId_Invoke_Test()
        {
            //Arrange
            IBankAccountManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IBankAccountManagementService>();
            Guid userId = Guid.Parse("ABCDF000-1234-5678-9000-123456789000");

            //Act
            List<BankAccount> result = target.FindBankAccountsByUserId(userId);

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(userId, result[0].UserId);
        }

        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void RemoveBankAccount_Invoke_NullItemThrowNewArgumentNullException_Test()
        {
            //Arrange
            IBankAccountManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IBankAccountManagementService>();

            //Act
            target.RemoveBankAccount(null);
        }

        /// <summary>
        ///A test for RemoveBankAccount
        ///</summary>
        [TestMethod()]
        public void RemoveBankAccount_Invoke_Test()
        {
            //Arrange
            IBankAccountManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IBankAccountManagementService>();
            Guid userId = Guid.Parse("ABCDF000-1234-5678-9000-123456789000");
            List<BankAccount> result = null;

            //Act
            result = target.FindBankAccountsByUserId(userId);
            target.RemoveBankAccount(result[0]);

            //Assert
            List<BankAccount> removed = target.FindBankAccountsByUserId(userId);
            Assert.IsNotNull(removed);
            Assert.AreEqual(0, removed.Count);

            BankAccount bankAccount = new BankAccount()
                    {
                        BankAccountId = Guid.Parse("ABCDF000-1234-5678-9000-1234567890EF"),
                        BankAccountNumber = "1",
                        UserId = Guid.Parse("ABCDF000-1234-5678-9000-123456789000"),
                        IsEnabled = true
                    };
            target.AddBankAccount(bankAccount);
        }

        /// <summary>
        ///A test for FindBankAccountById
        ///</summary>
        [TestMethod()]
        public void FindPagedBankAccounts_Invoke_Test()
        {
            //Arrange
            IBankAccountManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IBankAccountManagementService>();

            //Act 
            List<BankAccount> result = target.FindPagedBankAccounts(0,1000);

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
        }

        /// <summary>
        ///A test for GetAllBankAccountTypes
        ///</summary>
        [TestMethod()]
        public void GetAllBankAccountTypes_Invoke_Test()
        {
            //Arrange
            IBankAccountManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IBankAccountManagementService>();

            //Act 
            List<BankAccountType> result = target.GetAllBankAccountTypes();

            //Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count > 0);
        }

        /// <summary>
        ///A test for GetAllBanks
        ///</summary>
        [TestMethod()]
        public void GetAllBanks_Invoke_Test()
        {
            //Arrange
            IBankAccountManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IBankAccountManagementService>();

            //Act 
            List<Bank> result = target.GetAllBanks();

            //Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count > 0);
        }
    }
}
